package com.feige.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

import com.feige.pojo.SecurityUser;
import com.feige.pojo.dto.LoginFormDto;
import com.feige.pojo.dto.RegisterFormDto;
import com.feige.result.AjaxResult;
import com.feige.result.R;
import com.feige.service.IndexService;

import com.feige.openfeigh.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * @author feige
 */
@Api(tags = "主控制器类")
@RestController
@RequestMapping("/auth")
public class IndexController {

    @Resource
    private IndexService indexService;

    @Autowired
    private UserService userService;


    /**
     * 注册方法
     * @param registerFormDto 注册表单
     * @return 结果
     */
    @ApiOperation(value="注册")
    @PostMapping("/register")
    public AjaxResult register(@Validated @RequestBody RegisterFormDto registerFormDto)   {
        R<String> r = userService.checkEmailAuthorize(registerFormDto.getEmail());
        String email = null;
        if (r.getCode() == 200){
            email = r.getMsg();
        }
        if ("教师".equals(registerFormDto.getRole()) && StrUtil.isBlank(email)){
            return AjaxResult.error(5000,"该邮箱未授予教师权限，请选择学生角色或联系管理员获取授权");
        }
        SecurityUser securityUser = indexService.register(registerFormDto);
        if (ObjectUtil.isNotEmpty(securityUser)){
            return AjaxResult.success(securityUser);
        }else {
            return AjaxResult.error();
        }
    }


    /**
     * 登录方法
     * @param loginFormDto 登录表单
     * @return 结果
     */
    @ApiOperation(value="登录")
    @PostMapping("/login")
    public AjaxResult login(@Validated @RequestBody LoginFormDto loginFormDto)   {
        SecurityUser securityUser = indexService.login(loginFormDto);
        if (ObjectUtil.isNotEmpty(securityUser)){
            return AjaxResult.success(securityUser);
        }else {
            return AjaxResult.error();
        }
    }


    @ApiOperation(value = "退出登录")
    @GetMapping("/logout")
    public AjaxResult logout(@RequestHeader("token") String token){
        System.out.println(token);
        return indexService.logout(token);
    }


    @ApiOperation(value = "获取用户的信息")
    @GetMapping("/info")
    public AjaxResult getInfo(HttpServletRequest request){
        SecurityUser info = indexService.getInfo(request);
        if (!StringUtils.isEmpty(info)){
            return AjaxResult.success(info);
        }else {
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "发送邮箱验证码")
    @GetMapping("/sender/{email}")
    public AjaxResult senderEmailCode(@PathVariable String email){
        // 发送邮箱验证码
        boolean sender = indexService.senderEmailCode(email);
        if (sender){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "验证邮箱")
    @GetMapping("/validate/{email}/{code}")
    public AjaxResult validateEmail(@PathVariable String email, @PathVariable String code){
        // 验证邮箱
        indexService.validateEmail(email,code);
        return AjaxResult.success();
    }
}
